package com.yellowpage.ca.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.UserFunctionConfig;
import com.yellowpage.ca.service.UserFunctionConfigService;

public class UserFunctionConfigAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<UserFunctionConfig> userFunctionConfigList = new ArrayList<UserFunctionConfig>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private UserFunctionConfig userFunctionConfig;
	private UserFunctionConfigService userFunctionConfigService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;


		//负责查询的语句
		private String query_value;
		private String query_createUser;
		private String query_createTime;
		private String query_code;
		private String query_configId;
		private String query_userFunction;
		private String query_lastUpdateUser;
		private String query_lastUpdateTime;



	//给出对应的userfunctionID对应的字符串
	private Map userfunctionmap;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals(""))
		{
			this.userFunctionConfig = userFunctionConfigService.getUserFunctionConfig(Long.parseLong(selectedId));
		}

		//获得对应值
		userfunctionmap = userFunctionConfigService.getuserfunctionmap();
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("value", query_value);
	    	    queryMap.put("createUser", query_createUser);
	    	    queryMap.put("createTime", query_createTime);
	    	    queryMap.put("code", query_code);
	    	    queryMap.put("configId", query_configId);
	    	    queryMap.put("userFunction", query_userFunction);
	    	    queryMap.put("lastUpdateUser", query_lastUpdateUser);
	    	    queryMap.put("lastUpdateTime", query_lastUpdateTime);
	    	    	
		resultList = userFunctionConfigService.queryUserFunctionConfigByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {


		userFunctionConfigService.saveUserFunctionConfig(userFunctionConfig,(loginUser.getUserId()).intValue());
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    if (userFunctionConfig.getValue().length() == 0) {
			this.addActionError("** Value 不能为空 !");
		}
			    	    	    	    	    	    	    if (userFunctionConfig.getCode().length() == 0) {
			this.addActionError("** Code 不能为空 !");
		}
			    	    	    	    	    	    	    	    	    	    
		this.setValidatorResult("//UserFunctionConfigAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    userFunctionConfig = userFunctionConfigService.getUserFunctionConfig(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    userFunctionConfigService.updateUserFunctionConfig(userFunctionConfig);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    if (userFunctionConfig.getValue().length() == 0) {
			this.addActionError("** Value 不能为空 !");
		}
			    			    			    			    if (userFunctionConfig.getCode().length() == 0) {
			this.addActionError("** Code 不能为空 !");
		}
			    			    			    			    			    		this.setValidatorResult("//UserFunctionConfigUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.userFunctionConfigService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常");
			this.addActionError("");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setUserFunctionConfig(UserFunctionConfig userFunctionConfig) {
		this.userFunctionConfig = userFunctionConfig;
	}
	
	public UserFunctionConfig getUserFunctionConfig() {
		return userFunctionConfig;
	}
	 
	 
	 public void setUserFunctionConfigService(UserFunctionConfigService userFunctionConfigService) {
		this.userFunctionConfigService = userFunctionConfigService;
	}
	
	public List<UserFunctionConfig> getUserFunctionConfigList() {
		return userFunctionConfigList;
	}

	public void setUserFunctionConfigList(List<UserFunctionConfig> userFunctionConfigList) {
		this.userFunctionConfigList = userFunctionConfigList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_value() {
        return this.query_value;
    }
    
    public void setQuery_value(String value){
        this.query_value = value;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_code() {
        return this.query_code;
    }
    
    public void setQuery_code(String code){
        this.query_code = code;
    }    

        public String getQuery_configId() {
        return this.query_configId;
    }
    
    public void setQuery_configId(String configId){
        this.query_configId = configId;
    }    

        public String getQuery_userFunction() {
        return this.query_userFunction;
    }
    
    public void setQuery_userFunction(String userFunction){
        this.query_userFunction = userFunction;
    }    

        public String getQuery_lastUpdateUser() {
        return this.query_lastUpdateUser;
    }
    
    public void setQuery_lastUpdateUser(String lastUpdateUser){
        this.query_lastUpdateUser = lastUpdateUser;
    }    

        public String getQuery_lastUpdateTime() {
        return this.query_lastUpdateTime;
    }
    
    public void setQuery_lastUpdateTime(String lastUpdateTime){
        this.query_lastUpdateTime = lastUpdateTime;
    }

	public Map getUserfunctionmap() {
		return userfunctionmap;
	}

	public void setUserfunctionmap(Map userfunctionmap) {
		this.userfunctionmap = userfunctionmap;
	}




}
